package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "category",key = "#setmealDTO.categoryId")     //删除缓存
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除")
    @DeleteMapping()
    @CacheEvict(cacheNames = "category",allEntries = true)      //删除缓存 清理所有键值对
    public Result batchDelete(@RequestParam List<Long> ids) {
        setmealService.batchDelete(ids);
        return Result.success();
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "category",allEntries = true)      //删除缓存 清理所有键值对
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("菜品停售和启售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "category",allEntries = true)      //删除缓存 清理所有键值对
    public Result saleAndHaltSale(Long id,@PathVariable  Integer status){
        setmealService.saleAndHaltSale(id, status);
        return Result.success();
    }

   @ApiOperation("通过Id获取")
   @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

}
